package com.boful.common.file.model;

import java.io.InputStream;
import java.io.OutputStream;

/***
 * this is abstract file,support smb://,ftp://,http://,file://,hdfs://
 * 
 * @author ligson
 * 
 */
public class NetFile {

	private String url;
	private boolean isDirectory;
	private FileSystem fileSystem = null;

	public NetFile(String url) throws Exception {
		this.url = url;
		fileSystem = FileSystem.getFileSystem(url);
	}

	public String getName() {
		return fileSystem.getName();
	}

	public String getUrl() {
		return fileSystem.normalize(getUrl());
	}

	public String getScheme() {
		return fileSystem.getScheme();
	}

	public long getLength() {
		return fileSystem.getLength();
	}

	public boolean isExist() {
		return fileSystem.exist();
	}

	public boolean isDirectory() {
		return fileSystem.isDirectory();
	}

	public void setDirectory(boolean isDirectory) {
		this.isDirectory = isDirectory;
	}

	public InputStream getInputStream() {
		if (!isDirectory()) {
			return fileSystem.getInputStream();
		} else {
			return null;
		}

	}

	public OutputStream getOutputStream() {
		if (!isDirectory()) {
			return fileSystem.getOutputStream();
		} else {
			return null;
		}

	}

	public boolean exist() {
		return fileSystem.exist();
	}

	public boolean copyTo(NetFile destFile) {
		if ((!destFile.isDirectory) && (!isDirectory)) {
			return fileSystem.copyTo(destFile);
		} else {
			return false;
		}

	}

	/***
	 * mine type
	 * 
	 * @return mine type
	 */
	public String getContentType() {
		return fileSystem.getContentType();
	}

}
